package br.com.pixt;
 
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.ksoap2.serialization.SoapObject;

public class Inicio extends ListActivity 
{

	private static final String CATEGORIA = "ZAE";
	private static int ID_CLIENTE = 0;
	@Override
	public void onCreate(Bundle bundle) 
	{
		//Intent itdb =new Intent("FOLHAS")
		//startActivityForResult(new Intent(this, EditarCarro.class), INSERIR_EDITAR);
		super.onCreate(bundle);
		LinearLayout layout = new LinearLayout(this);
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setView(layout);

        Intent itParam = getIntent();	
		
		if (itParam!=null)
		{	
			Bundle params = itParam.getExtras();
			if (params!=null)
			{
				ID_CLIENTE = params.getInt("IdCliente");
			}
		}

		if (ID_CLIENTE!=0)
		{
			if (Acesso(null,null,null)==0)
			{
				
			}
		}
		else
		{	
			layout.setOrientation(1);
//			AlertDialog.Builder alert = new AlertDialog.Builder(this);
	
			alert.setTitle("Login");
			
			//Intent itAlarme = new Intent("AGENDAR_ALARME");
			//Bundle params = new Bundle();
			//params.putInt("Minutos", 1);
			
			//itAlarme.putExtras(params);
			//startActivity(itAlarme);
	
			final TextView txtDDD = new TextView(this);
			txtDDD.setText("DDD :");
			layout.addView(txtDDD);
	
			final EditText inputd = new EditText(this);
			inputd.setText("11");
			layout.addView(inputd);
	
			final TextView txtUser = new TextView(this);
			txtUser.setText("Cliente :");
			layout.addView(txtUser);
	
			final EditText input = new EditText(this);
			layout.addView(input);
			
			final TextView txtSenha = new TextView(this);
			txtSenha.setText("Senha :");
			layout.addView(txtSenha);
		
			final EditText inputs = new EditText(this);
			layout.addView(inputs);
			
			alert.setView(layout);
		
			alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
				{			
					public void onClick(DialogInterface dialog, int whichButton) 
					{					
						if (Acesso(txtUser.getText().toString(),txtSenha.getText().toString(),inputd.getText().toString())==1)
						{
							dialog.dismiss();
						}
						else
						{
							dialog.cancel();
						}
					}
				});
	
			alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() 
				{
					public void onClick(DialogInterface dialog, int whichButton) 
					{
	
						finish();
					}
				});
	
		}
		alert.show();
	}
	
	private int Acesso(String strCPF, String strSenha,String strDDD)
	{
		int intRet = 0;
		try 
		{
			AcessoWS login = new AcessoWS();
			final SoapObject result = login.GetCliente(strCPF, strSenha,ID_CLIENTE);
			String nome = new String();
			int id = 0;
			int intQtdeVeiculos = 0;
			int intQtdeHorasAbertas = 0;
			int intQtdeHorasFechadas = 0;
			int intQtdeMultas = 0;
			
 			if (result.getPropertyCount()>0)
			{
				for(int i =0; i < result.getPropertyCount(); i++)
				{
					SoapObject so = (SoapObject) result.getProperty(i);						
					nome=so.getProperty("StrNome").toString();
					id= Integer.parseInt(so.getProperty("IdCliente").toString());
					intQtdeVeiculos= Integer.parseInt(so.getProperty("IntQtdeVeiculos").toString());
					intQtdeHorasAbertas= Integer.parseInt(so.getProperty("IntQtdeHorasAbertas").toString());
					intQtdeHorasFechadas= Integer.parseInt(so.getProperty("IntQtdeHorasFechadas").toString());
					intQtdeMultas= Integer.parseInt(so.getProperty("IntQtdeMultas").toString());
				}
				Intent it =new Intent("ABRIR_SISTEMA");
				Bundle params = new Bundle();
				params.putString("StrNome", nome);
				params.putString("StrDDD",strDDD);
				params.putInt("IdCliente", id);
				params.putInt("IntQtdeVeiculos", intQtdeVeiculos);
				params.putInt("IntQtdeHorasAbertas", intQtdeHorasAbertas);
				params.putInt("IntQtdeHorasFechadas", intQtdeHorasFechadas);
				params.putInt("IntQtdeMultas", intQtdeMultas);
				
				it.putExtras(params);
				startActivity(it);					
			}
		} 
		catch (Exception e) 
		{
			Log.e(CATEGORIA, e.getMessage(), e);
		}
		finally 
		{
			intRet = 1;
		}

		return intRet;
	}


}